package com.ruoyi.collection.process;

import com.alibaba.fastjson.JSON;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.QueryOrderStatusReq;
import com.ruoyi.collection.domain.QueryOrderStatusResp;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代扣结果查询
 * @author Victor
 *
 */
@Slf4j
@Service("QueryOrderStatus")
public class QueryOrderStatusPorcessor implements CollectionProcess {
	
	@Autowired
	private IRepaymentOrderService repaymentOrderService;

	@Override
	public Object process(ApiParameter req) throws Exception {
		
		log.info("代扣结果查询开始");

		QueryOrderStatusReq reqBody = JSON.parseObject(req.getBody().toString(), QueryOrderStatusReq.class);
		QueryOrderStatusResp respBody = new QueryOrderStatusResp();
		Map<String, Map<String, Object>> orderStatusMap = new HashMap<String, Map<String, Object>>();
		respBody.setOrderStatusMap(orderStatusMap);
		
		for(String repaymentNo : reqBody.getOrderList()){
			
			RepaymentOrder orderCondition = new RepaymentOrder();
			orderCondition.setRepaymentNo(repaymentNo);
			List<RepaymentOrder> orderList = repaymentOrderService.selectRepaymentOrderList(orderCondition);
			
			if (orderList.size() != 1) {
	        	log.error("获取订单异常{}", repaymentNo);
	        	throw new Exception("获取订单异常");
	        }
			
			RepaymentOrder order = orderList.get(0);
			Map<String, Object> orderMap = new HashMap<String, Object>();
			
			// 响应赋值
        	if(BizConstants.REPAYMENT_ORDER_STATUS_SUCCESS == order.getRepayStatus()){
        		orderMap.put("PAYMENT_STATUS", "S");
        	}else if(BizConstants.REPAYMENT_ORDER_STATUS_DOING == order.getRepayStatus()){
        		orderMap.put("PAYMENT_STATUS", "P");
        	}else if(BizConstants.REPAYMENT_ORDER_STATUS_WAIT == order.getRepayStatus()){
        		orderMap.put("PAYMENT_STATUS", "I");
        	}else{
        		orderMap.put("PAYMENT_STATUS", "F");
        	}
        	orderMap.put("RESULT_MESSAGE", order.getRemark());
        	
        	orderStatusMap.put(order.getRepaymentNo(), orderMap);
		}
		
		log.info("代扣结果查询结束");
		
		return respBody;
	}

}
